package eerbaar.shared.internal.tree.policy;

import java.util.ArrayList;
import java.util.List;

import eerbaar.shared.tree.INode;
import eerbaar.shared.tree.NodeCmdFactory;
import eerbaar.shared.tree.policy.IPolicy;

public class SizePolicy implements IPolicy {

    @Override
    public void afterChildrenHaveChanged(INode node) {
        removeOldNodes(node);
    }

    private void removeOldNodes(INode node) {

        List<INode> list = new ArrayList<INode>();
        INode[] children = node.getChildren();
        int count = 0;
        for (int i = children.length - 1; i >= 0; i--) {
            INode child = children[i];

            if (count > 4) {
                list.add(child);
            }
            count++;
        }

        NodeCmdFactory.remove(node, list);
    }

    @Override
    public String toString() {
        return "size node policy";
    }

}
